﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QLKDS_WEB
{
    public partial class Publisher : System.Web.UI.Page
    {
        #region Khai bao
            SqlCommand cmd;
            SqlDataAdapter da;
            DataSet ds;
            SqlConnection con;
            string str = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Publisher_Load();
            }
        }

        private void Publisher_Load()
        {
            ds = new DataSet();
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("stPublisher_Load", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    con.Close();
                    gvPublisher.DataSource = ds.Tables[0];
                    gvPublisher.DataBind();
                }
                catch(Exception ex)
                {
                    WebMsgBox.Show(ex.Message);
                }
            }
        }

        protected void AddNew_click(object sender, EventArgs e)
        {
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("stPublisher_Insert", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    //cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = txtStatus.Text;
                    cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = "";
                    cmd.Parameters.Add("@CreatedDate", SqlDbType.NVarChar).Value = DateTime.Now;
                    cmd.Parameters.Add("@ModifiedBy", SqlDbType.NVarChar).Value ="";
                    cmd.Parameters.Add("@ModifiedDate", SqlDbType.NVarChar).Value ="";
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    WebMsgBox.Show(ex.Message);
                }
                Publisher_Load();
            }
        }

        protected void Edit_click(object sender, EventArgs e)
        {
            int key = (int)gvPublisher.DataKeys[gvPublisher.SelectedIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("stPublisher_Update", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = key;
                    //cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = txtStatus.Text;
                    cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.NVarChar).Value = "";
                    cmd.Parameters.Add("@CreatedDate", SqlDbType.NVarChar).Value = DateTime.Now;
                    cmd.Parameters.Add("@ModifiedBy", SqlDbType.NVarChar).Value = "";
                    cmd.Parameters.Add("@ModifiedDate", SqlDbType.NVarChar).Value = DateTime.Now;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    WebMsgBox.Show(ex.Message);
                }
                Publisher_Load();
            }
        }

        protected void refresh_click(object sender, EventArgs e)
        {
            btnRefresh.CausesValidation = true;
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            //txtName.Text = "";
            txtStatus.Text = "";
            txtDescription.Text = "";
        }

        protected void Cancel_click(object sender, EventArgs e)
        {

        }

        protected void gvPublisher_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int key = (int)gvPublisher.DataKeys[e.RowIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("stPublisher_Delete", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = key;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                {
                    WebMsgBox.Show(ex.Message);
                }
                Publisher_Load();
            }
        }

        protected void gvPublisher_SelectedIndexChanged(object sender, EventArgs e)
        {
            Edit.Enabled = true;
            int dong = gvPublisher.SelectedIndex;
            //txtName.Text = gvPublisher.Rows[dong].Cells[3].Text.ToString();
            txtStatus.Text = gvPublisher.Rows[dong].Cells[4].Text.ToString();
            txtDescription.Text = gvPublisher.Rows[dong].Cells[5].Text.ToString();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }
    }
}